//
//  LKAddDeviceByCodeVC.m
//  TJHYHardwareProduct
//
//  Created by Kendama on 2018/1/22.
//  Copyright © 2018年 Kendama. All rights reserved.
//

#import "HYAddDeviceByCodeVC.h"
#import "LKCustonTextFieldView.h"
#import "HYAddDeviceVC.h"


@interface HYAddDeviceByCodeVC ()

@property(nonatomic,strong)LKCustonTextFieldView *textFieldView;
@property(nonatomic,strong)UILabel *numberLabel;
@property(nonatomic,strong)UIButton *commitButton;

@end

@implementation HYAddDeviceByCodeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

#pragma mark --- 点击事件
- (void)didClickNextButton
{
    NSString *result = self.textFieldView.textField.text;
    if (![result isNumberString])
    {
        [SVProgressHUD showErrorWithStatus:@"设备编号只能是数字"];
        return;
    }
    
    if (result.length > 15)
    {
        [SVProgressHUD showErrorWithStatus:@"设备编号限制在15个字符长度"];
        return;
    }
    
    if ([result isNumberString] && result.length > 0 && result.length <= 15)
    {
        [self.textFieldView.textField resignFirstResponder];
        HYAddDeviceVC *vc = [HYAddDeviceVC new];
        vc.dno = result;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else
    {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的设备编号"];
        return;
    }
}

#pragma mark --- 懒加载
- (LKCustonTextFieldView *)textFieldView
{
    if(!_textFieldView)
    {
        UIView *superView = self.view;
        _textFieldView = [[LKCustonTextFieldView alloc]init];
        _textFieldView.textField.placeholder = @"请输入设备编号";
        _textFieldView.textField.keyboardType = UIKeyboardTypeNumberPad;
        [superView addSubview:_textFieldView];
    }
    return _textFieldView;
}
- (UIButton *)commitButton
{
    if(!_commitButton)
    {
        UIView *superView = self.view;
        _commitButton = [[UIButton alloc]init];
        [superView addSubview:_commitButton];
        [_commitButton setTitle:@"下一步" forState:UIControlStateNormal];
        [_commitButton.titleLabel setFont:fontWithSize(15)];
        [_commitButton setBackgroundImage:[UIImage imageNamed:@"button_login_nor"] forState:UIControlStateNormal];
        [_commitButton addTarget:self action:@selector(didClickNextButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _commitButton;
}

- (UILabel *)numberLabel
{
    if(!_numberLabel)
    {
        UIView *superView = self.view;
        _numberLabel = [[UILabel alloc]init];
        [superView addSubview:_numberLabel];
        [_numberLabel setFont:fontWithSize(15)];
        [_numberLabel setTextColor:[UIColor darkGrayColor]];
        _numberLabel.text = @"设备编号";
    }
    return _numberLabel;
}


#pragma mark --- 初始化布局
- (void)base_masLayoutSubview
{
    __weak UIView *superView = self.view;
    
    
    
    [self.textFieldView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(64+px1920Height(123));
        make.left.right.mas_equalTo(superView);
        make.height.mas_equalTo(px1920Height(136));
    }];
    
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(73));
        make.bottom.mas_equalTo(_textFieldView.mas_top).with.offset(-px1920Height(20));
    }];
    
    [self.commitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_textFieldView.mas_bottom).with.offset(px1920Height(147));
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(66));
        make.right.mas_equalTo(superView.mas_right).with.offset(-px1080Width(66));
        make.height.mas_equalTo(px1920Height(136));
    }];
    
    superView.backgroundColor = rgba(245,245,245,1);
}

#pragma mark --- 导航栏
- (void)base_configNavigationBar
{
    [self setNavigationTitle:@"设备添加" titleColor:[UIColor blackColor]];
    [self setLeftButtonImage:[UIImage imageNamed:@"left"]];
    self.navigationBar.backgroundColor = [UIColor whiteColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
